package com.cqie.tes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqie.tes.comm.R;
import com.cqie.tes.comm.qo.BaseQo;
import com.cqie.tes.comm.service.impl.BaseServiceImpl;
import com.cqie.tes.domain.Dept;
import com.cqie.tes.domain.qo.tes.DeptQo;
import com.cqie.tes.domain.vo.tes.dept.DeptOptionVo;
import com.cqie.tes.domain.vo.tes.dept.DeptVo;
import com.cqie.tes.mapper.DeptMapper;
import com.cqie.tes.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DD-GKD
 * @since 2020-09-18
 */
@Service
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public R getList(BaseQo query) {
        // 1
        DeptQo deptQo = (DeptQo) query;
        // 构建查询条件
        List<DeptVo> voList = new ArrayList<>();

        QueryWrapper<Dept> fqw = new QueryWrapper<>();
        fqw.eq("mark", 1);
        fqw.orderByAsc("sortCode");
        List<Dept> entityList = deptMapper.selectList(fqw);

        entityList.forEach(entity -> {
            DeptVo vo = new DeptVo();
            vo.setId(entity.getId());
            vo.setParentId(entity.getParentId());
            vo.setName(entity.getName());
            vo.setSortCode(entity.getSortCode());

            voList.add(vo);
        });
        return R.ok().message("查询成功").data("total", voList.size()).data("rows", voList);
    }


    @Override
    public R edit(Dept entity) {
        return super.edit(entity);
    }

    // TODO 删除有问题
    @Override
    public R deleteById(Integer id) {
        if (id == null || id == 0) {
            return R.error().message("记录ID不能为空");
        }
        Dept entity = this.getById(id);
        if (entity == null) {
            return R.error().message("记录不存在");
        }
        return super.delete(entity);
    }


    public List<Dept> search(int i, List<DeptOptionVo> vos, List<Dept> entityList){
        for (Dept entity:entityList
             ) {
            DeptOptionVo vo = new DeptOptionVo();
            // 构建结构
            StringBuffer typeBuffer = new StringBuffer();
            for (int j = 0;j<i;j++){
                typeBuffer.append("∟");
            }
            i++;
            vo.setId(entity.getId());
            vo.setName(typeBuffer.toString()+entity.getName());
            // 先加是为了顺序
            vos.add(vo);

            QueryWrapper<Dept> cqw = new QueryWrapper<>();
            cqw.eq("mark", 1);
            cqw.eq("parentId", entity.getId());
            cqw.orderByAsc("sortCode");
            List<Dept> depts = deptMapper.selectList(cqw);
            search(i,vos,depts);
            i--;
        }
        return null;
    }



    @Override
    public R optionList() {
        // 结果列表
        List<DeptOptionVo> vos  =new ArrayList<>();
        // 一级菜单
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq("mark", 1);
        qw.eq("parentId", 0);
        qw.orderByAsc("sortCode");

        List<Dept> entityList = deptMapper.selectList(qw);

        int i = 0;
        search(i,vos,entityList);
        return R.ok().message("查询成功").data("options", vos);
    }
}
